package cn.chrilwe.pigpig.user.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.ribbon.proxy.annotation.Http.HttpMethod;

import cn.chrilwe.pigpig.core.base.BaseController;
import cn.chrilwe.pigpig.core.model.user.User;
import cn.chrilwe.pigpig.core.model.user.ext.UserExt;
import cn.chrilwe.pigpig.core.response.BaseResult;
import cn.chrilwe.pigpig.user.model.UpdatePasswordRequest;
import cn.chrilwe.pigpig.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author chrilwe
 * 2020-8-5
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户中心接口API")
public class UserController extends BaseController {
	
	@Autowired
	private UserService userService;
	
	@PostMapping("/addUser")
	@ApiOperation(tags = "注册用户", value = "addUser")
	public BaseResult addUser(@RequestBody User user) {
		
		return null;
	}
	
	@GetMapping("/queryByUsername")
	@ApiOperation(tags = "根据账号查询用户", value = "queryByUserName")
	@ApiImplicitParam(dataTypeClass = String.class, name = "username")
	public UserExt queryByUserName(@RequestParam(value = "username", defaultValue = "")String username) {
		if(StringUtils.isEmpty(username)) {
			return null;
		}
		return userService.findUserExtByUsername(username);
	}
	
	
	@PostMapping("/update/password")
	@ApiOperation(tags = "修改用户密码", value = "updatePassword")
	public BaseResult updatePassword(@RequestBody UpdatePasswordRequest updateRequest) {
		String newPassword = updateRequest.getNewPassword();
		String oldPassword = updateRequest.getOldPassword();
		String username = updateRequest.getUsername();
		return userService.updatePassword(oldPassword, newPassword, username);
	}
}
